<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>多图片上传</title>
	<link rel="stylesheet" type="text/css" href="__ROOT__/public/xiaoniao/css/style.css"/>
</head>

<body>

    <div class="container">
        <!--    照片添加    -->
        <div class="z_photo">
            <div class="z_file">
                <input type="file" name="file" id="file" value="" accept="image/*" multiple onchange="imgChange('z_photo','z_file');" />
            </div>

        </div>
    </div>

    <script src="__ROOT__/public/xiaoniao/js/jquery-1.8.3.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="__ROOT__/public/xiaoniao/js/ajaxfileupload.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript">
        //px转换为rem
        (function(doc, win) {
            var docEl = doc.documentElement,
                resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
                recalc = function() {
                    var clientWidth = docEl.clientWidth;
                    if (!clientWidth) return;
                    if (clientWidth >= 640) {
                        docEl.style.fontSize = '100px';
                    } else {
                        docEl.style.fontSize = 100 * (clientWidth / 640) + 'px';
                    }
                };

            if (!doc.addEventListener) return;
            win.addEventListener(resizeEvt, recalc, false);
            doc.addEventListener('DOMContentLoaded', recalc, false);
        })(document, window);

        function imgChange(obj1, obj2) {
        	$.ajaxFileUpload({
                url: 'http://www.caizhanzhushou.com/index.php?g=wechat&m=zhuan&a=ajaximg', //用于文件上传的服务器端请求地址
                type:'post',
                secureuri: false, //一般设置为false
                fileElementId: 'file', //文件上传控间的id属性  <input type="file" id="Image" name="Image" />
                dataType: 'JSON', //返回值类型 一般设置为json
                success: function (data, status){
                    var file = document.getElementById("file");
//		            //存放图片的父级元素
		            var imgContainer = document.getElementsByClassName(obj1)[0];
//		            //获取的图片文件
		            var fileList = file.files;
//		            //文本框的父级元素
		            var input = document.getElementsByClassName(obj2)[0];
		            var imgArr = [];
//		            //遍历获取到得图片文件
		            for (var i = 0; i < fileList.length; i++) {
		            		var imgUrl = window.URL.createObjectURL(file.files[i]);
			                imgArr.push(imgUrl);
			                var img = document.createElement("img");
			                img.setAttribute("src", imgArr[i]);
			                var imgAdd = document.createElement("div");
			                var imgdel=document.createElement("img");
			                imgdel.setAttribute("src",'image/z_del.png');
			                imgdel.setAttribute("class", "z_delImg");
			                imgAdd.setAttribute("class", "z_addImg");
			                imgAdd.appendChild(img);
			                imgAdd.appendChild(imgdel);
			                imgContainer.appendChild(imgAdd);
		            	}
		            imgRemove();
                },
                error: function (data, status, e){
                    alert('上传图片失败');
                }
            })
        };

        function imgRemove() {
            var imgList = $(".z_addImg"); 
            var imgList1 = $(".z_delImg");
            $(".z_delImg").each(function(){
            	$(this).click(function(){
            		$(this).parent().hide();
            	})
            });
        };

    </script>
</body>

</html>
